const express = require('express');
const router = express.Router();
const movieController = require('../controllers/movieController');
const { isAdmin } = require('../middlewares/authMiddleware');
const uploadController = require('../controllers/uploadController');
const multer = require('multer');
// 配置 Multer 以处理文件上传
const storage = multer.memoryStorage(); // 将文件存储在内存中
const fileFilter = (req, file, cb) => {
  // 检查文件类型，只允许图片上传
  if (file.mimetype.startsWith('image/')) {
    cb(null, true);
  } else {
    cb(new Error('只支持上传图片文件'), false);
  }
};
const upload = multer({ storage, fileFilter });

/**
 * @swagger
 * tags:
 *   name: Movies
 *   description: 电影相关的 API
 */

// 上传电影海报
/**
 * @swagger
 * /movies/upload:
 *   post:
 *     summary: 上传电影海报（管理员）
 *     tags: [Movies]
 *     description: 上传图片文件到服务器（支持 JPG/PNG 等图片格式）
 *     security:
 *       - bearerAuth: []
 *     consumes:
 *       - multipart/form-data
 *     parameters:
 *       - in: formData
 *         name: poster
 *         type: file
 *         required: true
 *         description: 上传的电影海报图片文件（最大支持 5MB）
 *         accept: image/*
 *     responses:
 *       200:
 *         description: 图片上传成功
 *         content:
 *           application/json:
 *             example:
 *               code: 200
 *               message: "图片上传成功"
 *               data:
 *                 url: "https://storage.example.com/bucket/1234567890-poster.jpg"
 *       400:
 *         description: 无效请求
 *         content:
 *           application/json:
 *             example:
 *               error: "未上传任何文件"
 *       401:
 *         description: 未授权访问
 *       500:
 *         description: 图片上传失败
 *         content:
 *           application/json:
 *             example:
 *               error: "图片上传失败"
 */
router.post('/upload', upload.single('poster'), uploadController.uploadImage);

// 获取所有电影
/**
 * @swagger
 * /movies:
 *   get:
 *     summary: 获取所有电影
 *     tags: [Movies]
 *     responses:
 *       200:
 *         description: 成功获取电影列表
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 title: "电影示例"
 *                 description: "电影描述"
 *                 rating: 8.5
 *       500:
 *         description: 服务器错误
 */
router.get('/', movieController.getAllMovies);

// 根据ID获取电影
/**
 * @swagger
 * /movies/{id}:
 *   get:
 *     summary: 根据ID获取电影详情
 *     tags: [Movies]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功获取电影详情
 *         content:
 *           application/json:
 *             example:
 *               id: 1
 *               title: "电影示例"
 *               description: "详细描述"
 *               rating: 8.5
 *               reviews_count: 100
 *       404:
 *         description: 电影不存在
 *       500:
 *         description: 服务器错误
 */
router.get('/:id', movieController.getMovieById);

// 搜索结果分页电影
/**
 * @swagger
 * /movies/search:
 *   post:
 *     summary: 搜索电影（分页）
 *     tags: [Movies]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 example: "复仇者"
 *               tag:
 *                 type: string
 *                 example: "动作"
 *               startYear:
 *                 type: integer
 *                 example: 2010
 *               endYear:
 *                 type: integer
 *                 example: 2020
 *               page:
 *                 type: integer
 *                 default: 1
 *               limit:
 *                 type: integer
 *                 default: 5
 *     responses:
 *       200:
 *         description: 搜索成功
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 title: "复仇者联盟"
 *                 rating: 8.5
 *       500:
 *         description: 服务器错误
 */
router.post('/search', movieController.searchMovies);

// 按评分排序获取电影
/**
 * @swagger
 * /movies/rank/rating:
 *   post:
 *     summary: 按评分排序获取电影（分页）
 *     tags: [Movies]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               page:
 *                 type: integer
 *                 default: 1
 *               limit:
 *                 type: integer
 *                 default: 5
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 title: "高分电影"
 *                 rating: 9.5
 *       500:
 *         description: 服务器错误
 */
router.post('/rank/rating', movieController.getMoviesByRating);

// 按评论数排序获取电影
/**
 * @swagger
 * /movies/rank/reviewsCount:
 *   post:
 *     summary: 按评论数排序获取电影（分页）
 *     tags: [Movies]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               page:
 *                 type: integer
 *                 default: 1
 *               limit:
 *                 type: integer
 *                 default: 5
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 title: "热门电影"
 *                 reviews_count: 500
 *       500:
 *         description: 服务器错误
 */
router.post('/rank/reviewsCount', movieController.getMoviesByReviewsCount);

// 创建电影
/**
 * @swagger
 * /movies:
 *   post:
 *     summary: 创建新电影（管理员）
 *     tags: [Movies]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - title
 *               - release_year
 *             properties:
 *               title:
 *                 type: string
 *                 example: "新电影"
 *               description:
 *                 type: string
 *                 example: "电影描述"
 *               poster_url:
 *                 type: string
 *                 example: "https://example.com/poster.jpg"
 *               rating:
 *                 type: number
 *                 example: 8.5
 *               release_year:
 *                 type: integer
 *                 example: 2023
 *               tags:
 *                 type: array
 *                 items:
 *                   type: string
 *                 example: [1, 2]
 *     responses:
 *       201:
 *         description: 电影创建成功
 *         content:
 *           application/json:
 *             example:
 *               message: "电影添加成功"
 *               movieId: 1
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.post('/', isAdmin, movieController.addMovie);

// 更新电影
/**
 * @swagger
 * /movies/{id}:
 *   put:
 *     summary: 更新电影信息（管理员）
 *     tags: [Movies]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *               description:
 *                 type: string
 *               poster_url:
 *                 type: string
 *               rating:
 *                 type: number
 *               release_year:
 *                 type: integer
 *               tags:
 *                 type: array
 *                 items:
 *                   type: string
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             example:
 *               message: "电影更新成功"
 *       401:
 *         description: 未授权
 *       404:
 *         description: 电影不存在
 *       500:
 *         description: 服务器错误
 */
router.put('/:id', isAdmin, movieController.updateMovie);

// 删除电影
/**
 * @swagger
 * /movies/{id}:
 *   delete:
 *     summary: 删除电影（管理员）
 *     tags: [Movies]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             example:
 *               message: "电影及对应评论和标签关系删除成功"
 *       401:
 *         description: 未授权
 *       404:
 *         description: 电影不存在
 *       500:
 *         description: 服务器错误
 */
router.delete('/:id', isAdmin, movieController.deleteMovie);

module.exports = router;